package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class GrassInRain extends Sprite
	{
		public function GrassInRain()
		{
			var N:Number = 6;
			function line(x1:Number ,y1:Number ,x2:Number ,y2:Number ,size:Number ,alp:Number ):void
			{
				graphics.lineStyle(size, 0x009900, alp);
				graphics.moveTo(x1, y1);
				graphics.lineTo(x2, y2);
			}
			
			function koch(ax:Number ,ay:Number ,size, len,ang,alp,n ):void
			{
				if (n<N)
				{
					var bx:Number=ax+len*Math.cos(ang);
					var by:Number=ay+len*Math.sin(ang);
					var ex:Number =ax+1/3*(bx-ax);
					var ey:Number =ay+1/3*(by-ay);
					var ang0:Number=Math.PI/6;
					var fx:Number=ex+1/6*len*Math.cos(ang-ang0*Math.random());
					var fy:Number=ey+1/6*len*Math.sin(ang-ang0*Math.random());
					var cx:Number=ax+len*Math.cos(ang);
					var cy:Number=ay+len*Math.sin(ang);
					var dx:Number=cx+1/4*len*Math.cos(ang+ang0*Math.random());// 逆时针的角度是负
					var dy:Number=cy+1/4*len*Math.sin(ang+ang0*Math.random());//
					line(ax,ay,ex,ey,size,alp);//线段ab
					line(ex,ey,cx,cy,size,alp);//ce
					line(ex,ey,fx,fy,size,alp);// 线段ef
					line(cx,cy,dx,dy,size,alp);// 线段cd
					var ang1:Number=Math.atan2(fy-ey,fx-ex);
					var ang2:Number=Math.atan2(dy-cy,dx-cx);
					koch(ex,ey,size*.8,len*.8,ang,alp*.5,n+1 );
					koch(ax,ay,size*.8,len*.8,ang,alp*.5,n+1 );
					koch(ex,ey,size*.8,len*.8,ang1,alp*.5,n+1 );
					koch(cx,cy,size*.8,len*.8,ang2,alp*.5,n+1 );
				}
			}
			
			function initf():void
			{
				for (var k:int=0; k<10; k++)
				{
					koch(k*50+50,500,1*Math.random()+2,50*Math.random()+30,-Math.PI/ 2,1,0);
				}
			}
			
			addEventListener(Event.ENTER_FRAME, ff);
			addEventListener(MouseEvent.CLICK ,f);
			
			function f(e):void
			{
				if (N <= 10)
				{
					N++;
				}
				else
				{
					N = 0;
				}
				addEventListener(Event.ENTER_FRAME, ff);
			}
			
			function ff(e):void
			{
				graphics.clear();
				initf();
				xiayu();
			}
			
			function xiayu():void
			{
				for (var k:int=0; k<100; k++)
				{
					graphics.lineStyle(1, 0xffffff, .4);
					var xx:Number = 800*Math.random() - 50;
					var yy:Number = 600*Math.random();
					var x0:Number=xx+10;
					var y0:Number=yy+10;
					graphics.moveTo(xx,yy);
					graphics.lineTo(x0,y0);
				}
			}
		}
	}
}